package com.wxp.alice.lanfilemaster.Utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;

public class NetworkUtils {
    // 连接的网络类型
    public enum NetworkType {
        WIFI,   // Wi-Fi
        MOBILE, // 流量
        NONE    // 未联网
    }

    /**
     * 获取设备连接的网络类型（Wi-Fi，流量，未连接）
     *
     * @param context 上下文
     * @return WIFI or MOBILE or NONE
     */
    public static NetworkType getNetworkType(Context context) {
        ConnectivityManager connectivityManager =
                (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        if (connectivityManager == null) {
            return NetworkType.NONE;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            // Android 10 (API 29) 及以上
            Network network = connectivityManager.getActiveNetwork();
            if (network == null) {
                return NetworkType.NONE;
            }
            NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network);
            if (capabilities == null) {
                return NetworkType.NONE;
            }

            if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
                return NetworkType.WIFI;
            } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
                return NetworkType.MOBILE;
            } else {
                return NetworkType.NONE;
            }
        } else {
            // Android 10 以下
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
            if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {
                if (activeNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                    return NetworkType.WIFI;
                } else if (activeNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                    return NetworkType.MOBILE;
                } else {
                    return NetworkType.NONE;
                }
            } else {
                return NetworkType.NONE;
            }
        }
    }

    /**
     * 判断是否有网络连接
     *
     * @param context 上下文
     * @return true: 已连接，false: 未连接
     */
    public static boolean isNetworkConnected(Context context) {
        return getNetworkType(context) != NetworkType.NONE;
    }

    /**
     * 判断是否连接了Wi-Fi
     *
     * @param context 上下文
     * @return true: 已连接，false: 未连接
     */
    public static boolean isWifiConnected(Context context) {
        return getNetworkType(context) == NetworkType.WIFI;
    }

    /**
     * 判断是否连接了流量
     *
     * @param context 上下文
     * @return true: 已连接，false: 未连接
     */
    public static boolean isMobileConnected(Context context) {
        return getNetworkType(context) == NetworkType.MOBILE;
    }

    /**
     * 获取当前连接的 Wi-Fi SSID
     *
     * @param context 上下文
     * @return Wi-Fi SSID
     */
    public static String getCurrentWifiSSID(Context context) {
        ConnectivityManager connectivityManager =
                (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            Network network = connectivityManager.getActiveNetwork();
            if (network != null) {
                NetworkCapabilities capabilities =
                        connectivityManager.getNetworkCapabilities(network);
                if (capabilities != null && capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
                    WifiManager wifiManager =
                            (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                    String ssid = wifiInfo.getSSID();
                    if (ssid != null && ssid.startsWith("\"") && ssid.endsWith("\"")) {
                        ssid = ssid.substring(1, ssid.length() - 1);
                    }
                    return ssid;
                }
            }
        } else {
            // For Android < Q
            WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            if (wifiManager != null) {
                WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                String ssid = wifiInfo.getSSID();
                if (ssid != null && ssid.startsWith("\"") && ssid.endsWith("\"")) {
                    ssid = ssid.substring(1, ssid.length() - 1);
                }
                return ssid;
            }
        }
        return "未连接 WiFi";
    }
}
